<template>
  <div class="zhiboxitong">
    <top></top>
    <div class="flex zhiboxitong_content">
      <div class="left">
        <left></left>
      </div>
      <div class="right" style="position: relative;">
        <div @click="goBack" class="backType">
          <img src="../../static/img/back.png" alt />
          直播销售 /
          <span style="color:#005AFF;margin-left:5px">销售页分类管理</span>
        </div>
        <div class="zhiboxitong_right">
          <div class="top_title">
            <div style="font-size:24px;color:#272F53;margin-left:20px">推广管理</div>
            <div class="addPromoter" @click="addPromoter">添加推广人</div>
          </div>
          <div class="top_style">
            <div style="width:100%;">
              <div class="search_container">
                <div style="margin-left:20px;" class="time_search">
                  <div style="margin:12px 0 8px 28px;font-size:14px">时间检索</div>
                  <!-- <el-date-picker
                    v-model="info.time"
                    :picker-options="pickerOptions"
                    clearable
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetimerange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    style="margin-left:25px"
                  ></el-date-picker>-->
                  <el-date-picker
                    v-model="info.time"
                    type="daterange"
                    align="left"
                    value-format="yyyy-MM-dd"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :picker-options="pickerOptions"
                  ></el-date-picker>
                </div>
                <div>
                  <el-button icon="el-icon-search" class="btn_search" round @click="search">检索</el-button>
                  <el-button class="btn_search" round @click="xiazai">下载全部明细</el-button>
                </div>
              </div>
            </div>
          </div>
          <div class="zhiboxitong_table">
            <el-table
              :data="list"
              style="width: 100%"
              :header-cell-style="{fontSize: '14px', backgroundColor: '#F8F9FD' }"
              :cell-style="{fontSize: '14px', backgroundColor: '#F8F9FD' }"
            >
              <el-table-column prop="name" label="推广人" width="200px"></el-table-column>
              <el-table-column prop="permission_name" label="部门/岗位" width="200px"></el-table-column>
              <el-table-column prop="user_name" label="推广链接" align="left">
                <template slot-scope="scope">
                  <div class="operate">
                    <div @click="qrCodeHandle(scope.row.id)">二维码</div>
                    <div @click="linkHandle(scope.row.id)" style="margin-left:15px">专属链接</div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="uv" label="访问人数" align="left" width="200px"></el-table-column>
              <el-table-column prop="sales" label="下单数量" align="left"></el-table-column>
              <el-table-column label="操作" width="400px" align="left">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    :disabled="scope.row.loading"
                    type="text"
                    @click="exportLoad(scope.row)"
                    style="color:#005AFF;font-size:14px"
                  >
                    {{ scope.row.loading ? '下载中...' : '下载明细' }}
                    <i
                      v-if="scope.row.loading"
                      class="el-icon-loading"
                    ></i>
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    @click="dele(scope.row.id)"
                    style="color:#005AFF;font-size:14px"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>

    <el-dialog
      title="二维码"
      :visible.sync="codeModal"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="qrcode" ref="qrCodeUrl" style="margin: 0 auto;width:200px"></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="codeModal = false" style="border-radius: 10px;">关闭</el-button>
        <el-button
          type="primary"
          @click="downLoad"
          style="background:#3158F1;border-radius: 10px;"
        >下载</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="addPromoterModal"
      width="500px"
      :showClose="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div class="checkProperty">
        <div style="margin-left:40px">选择人员:</div>
        <el-select
          filterable
          v-model="paramsInfo.user_id"
          placeholder="请选择"
          style="margin-left:30px"
        >
          <el-option
            v-for="item in promoterInfo"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="confim" style="border-radius: 10px;">取消</el-button>
        <el-button
          type="primary"
          @click="besureClick"
          style="background:#3158F1;border-radius: 10px;"
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
import axios from "axios";
import moment from "moment";
import top from "./components/top.vue";
import left from "./components/left.vue";
export default {
  components: {
    top,
    left
  },
  name: "plugManage",
  data() {
    return {
      addPromoterModal: false,
      codeModal: false,
      title: "",
      loading: false,
      list: [],
      promoterInfo: [],
      info: {
        name: "",
        start: "",
        end: "",
        time: []
      },
      paramsInfo: {
        user_id: "",
        store_id: ""
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  created() {
    this.getTableData();

    this.info.time = [
      moment()
        .subtract(7, "days")
        .format("YYYY-MM-DD"),
      moment().format("YYYY-MM-DD")
    ];
    this.info.start = this.info.time[0];
    this.info.end = this.info.time[1];
  },
  mounted() {},
  methods: {
    xiazai:function(){
      axios
        .get(`${this.apis}/api/promotion/order/export_all_data?start=${this.info.start}&end=${this.info.end}&store_id=${this.$route.query.id}`)
        .then(response => {
          if (response.data.msg.code == 0) {
            window.location.href = location.origin + '/' + response.data.data.path
          } else {
            this.$message.error(response.data.msg.info);
          }
        });
    },
    /* 商城推广列表 */
    getTableData() {
      const id = this.$route.query.id;
      axios
        .get(`${this.apis}/api/store/promotion/list`, {
          params: { start: this.info.start, end: this.info.end, store_id: id }
        })
        .then(response => {
          if (response.data.msg.code == 0) {
            this.list = response.data.data;
          } else {
            this.$message.error(response.data.msg.info);
          }
        });
      /* 获取推广人 */
      axios.get(`${this.apis}/api/user/list`).then(response => {
        if (response.data.msg.code == 0) {
          this.promoterInfo = response.data.data;
        } else {
          this.$message.error(response.data.msg.info);
        }
      });
    },
    /* 新增推广人 */
    besureClick() {
      const id = this.$route.query.id;
      const requestData = {
        ...this.paramsInfo,
        store_id: id
      };
      axios
        .post(`${this.apis}/api/store/promotion/add`, requestData)
        .then(response => {
          if (response.data.msg.code == 0) {
            this.getTableData();
            this.$message.success("添加成功");
            this.addPromoterModal = false;
            this.resetForm();
          } else {
            this.$message.error(response.data.msg.info);
          }
        });
    },
    /* 删除 */
    dele(id) {
      this.$confirm("是否要删除此条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        axios
          .post(`${this.apis}/api/store/promotion/del`, { id: id })
          .then(response => {
            if (response.data.msg.code == 0) {
              this.getTableData();
              this.$message.success("删除成功");
            } else {
              this.$message.error(response.data.msg.info);
            }
          });
      });
    },
    qrCodeHandle(id) {
      console.log("promoter_id===", id);
      console.log("id===", this.$route.query.id);
      this.codeModal = true;
      this.$nextTick(() => {
        this.$refs.qrCodeUrl.innerHTML = "";
        let url =
          location.origin +
          "/h5/index.html?id=" +
          this.$route.query.id +
          "&promoter_id=" +
          id;
        var qrcode = new QRCode(this.$refs.qrCodeUrl, {
          text: url, // 需要转换为二维码的内容
          width: 400,
          height: 400,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H
        });
      });
    },
    downLoad() {
      let img = document.getElementsByClassName("qrcode")[0].childNodes[1];
      var a = document.createElement("a");
      a.download = "erweima";
      a.href = img.src;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    },
    linkHandle(id) {
      let url =
        location.origin +
        "/h5/index.html?id=" +
        this.$route.query.id +
        "&promoter_id=" +
        id;
      this.copy(url);
    },
    /*
    导出
    */
    exportLoad(row) {
      console.log(this.info.start, this.info.end, "1111");
      row.loading = true;
      const params = new URLSearchParams({
        promoter_id: row.id,
        promoter_name: row.name,
        start: this.info.start,
        end: this.info.end
      });
      /* 这边是判断环境 */
      let addressName = "store.91haoka.cn";
      if (window.location.href.includes("store.91haoka.cn")) {
        addressName = "store.91haoka.cn";
      } else {
        addressName = "storep.91haoka.cn";
      }
      axios({
        method: "get",
        url: `${this.apis}/api/promotion/order/export`,
        params: params
      })
        .then(response => {
          if (response.data.msg.code == 0) {
            const filePath = response.data.data.path;
            const fileName = filePath.split("/")[3];
            const downPath = `https://${addressName}/${filePath}`;
            // const downPath = `https://store.91haoka.cn/${filePath}`;
            // console.log('https://store.91haoka.cn/${filePath}',`https://store.91haoka.cn/${filePath}`);
            const link = document.createElement("a");
            link.href = downPath;
            link.setAttribute("download", fileName); // 设置下载文件名
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            row.loading = false;
          } else {
            row.loading = false;

            this.$message.error(response.data.msg.info);
          }
        })
        .catch(error => {
          console.error("There was an error!", error);
          this.$message.error(response.data.msg.info);
          row.loading = false;
        });
    },
    goBack() {
      this.$router.go(-1);
    },
    addPromoter() {
      this.title = "添加推广人";
      this.addPromoterModal = true;
    },
    confim() {
      this.paramsInfo = {};
      this.addPromoterModal = false;
    },
    resetForm() {
      this.paramsInfo = {};
    },
    search() {
      if (this.info.time && this.info.time.length > 0) {
        this.info.start = this.info.time[0];
        this.info.end = this.info.time[1];
      } else {
        this.info.start = "";
        this.info.end = "";
      }
      this.getTableData();
    }
  }
};
</script>
<style>
/* 修改mssageBox样式 */
.custom-confirm-class .el-message-box__title {
  font-size: 20px;
  font-weight: bold;
}
</style>
<style scoped lang="less">
/deep/ .el-pagination {
  margin-right: 30px;
}
/deep/ .el-table {
  background-color: #f8f9fd;
}

// 划过改变table表格背景颜色
/deep/ .el-table__body tr:hover td {
  background-color: white !important;
}

/deep/ .el-table__body tr td:first-child {
  border-radius: 20px 0 0 20px;
  padding-left: 10px;
  box-sizing: border-box;
}
/deep/ .el-table__body tr td:last-child {
  border-radius: 0 20px 20px 0;
}
/deep/ .el-table__body td {
  height: 72px;
}
.jinggao_image {
  margin-right: 5px;
  width: 20px;
  height: 20px;
}
.tbym_box {
  display: flex;
  align-items: center;
}
.tflc_images {
  display: flex;
  justify-content: center;
  align-items: center;
}
.top_style {
  width: 100%;
  padding-top: 20px;
  box-sizing: border-box;
}

//去掉每行的下边框
/deep/ .el-table td.el-table__cell,
/deep/ .el-table th.el-table__cell.is-leaf {
  border-bottom: none;
}

// 去掉表格单元格边框
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
// 表格最外边框
.el-table--border,
.el-table--group {
  border: none;
}
// 头部边框
.customer-table thead tr th.is-leaf {
  border: 1px solid #ebeef5;
  border-right: none;
}
.customer-table thead tr th:nth-last-of-type(2) {
  border-right: 1px solid #ebeef5;
}
// 表格最外层边框-底部边框
.el-table--border::after,
.el-table--group::after {
  width: 0;
}
.customer-table::before {
  width: 0;
}
.customer-table .el-table__fixed-right::before,
.el-table__fixed::before {
  width: 0;
}
// 表格有滚动时表格头边框
.el-table--border th.gutter:last-of-type {
  border: 1px solid #ebeef5;
  border-left: none;
}
// 去除边框线
/deep/ .el-table::before {
  height: 0;
}
.search_container {
  width: 100%;
  display: flex;
  height: 120px;
  background: #f8f9fd;
  align-items: center;
  margin-bottom: 20px;
  border-radius: 20px 0 0 20px;
}
.flex_style {
  padding: 0 20px;
  box-sizing: border-box;
  margin: 20px 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.zhiboxitong {
  // background: #f2f2f2;
  min-height: 100vh;
  * {
    box-sizing: border-box;
  }
  .zhiboxitong_content {
    height: calc(100vh - 70px);
    .left {
      width: 210px;
      background: #fafafe;
      height: 100%;
    }
    .right {
      width: calc(100vw - 220px);
      padding-left: 20px;
      height: 100vh;
      overflow: auto;
      .zhiboxitong_right {
        height: calc(100vh - 90px);
        background: #fff;
        padding-top: 30px;
      }

      .zhiboxitong_table {
        padding: 20px 30px 0 30px;
        box-sizing: border-box;
        background: #f8f9fd;
        border-top-left-radius: 30px;
      }
    }
  }

  .flex {
    display: flex;
    flex-wrap: wrap;
  }

  .flexb {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .flexc {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
  }

  .flexbc {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
  }

  .flexcc {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start !important;
    align-items: center;
  }
}
/deep/ .el-dialog {
  border-radius: 20px;
}

/deep/ .el-input__inner {
  border-radius: 12px;

  box-shadow: none !important;
}

/deep/ .el-dialog__title {
  font-size: 20px;
  font-weight: bold; /* 修改标题字体大小 */
}
.left-align-text {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start !important;
  align-items: center;
}
.btn_search {
  background: #f72c50;
  border: 0;
  color: #fff;
  margin: 35px 0 0 30px;
}
/deep/ .el-pagination .el-pager li {
  border-radius: 12px; /* 设置圆角大小 */
  background: white;
}
.top_title {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.addPromoter {
  width: 120px;
  height: 42px;
  color: white;
  background: #f72c50;
  border-radius: 16px;
  font-size: 15px;
  line-height: 42px;
  text-align: center;
  margin-right: 20px;
  cursor: pointer;
}
.backType {
  display: flex;
  margin: 20px;
  align-items: center;
  font-size: 15px;
  cursor: pointer;
  img {
    font-size: 8px;
    height: 14px;
    margin-right: 10px;
  }
}
.operate {
  display: flex;
  cursor: pointer;
  color: rgb(0, 90, 255);
}
.checkProperty {
  display: flex;
  align-items: center;
}
/deep/ .time_search .el-input__inner {
  width: 420px !important;
  margin-left: 25px;
}
</style>
